<template>
  <el-row>
    <el-col :span="5" style="padding-left: 20px; padding-top: 20px; border-right: 1px solid #dfe6ec">
      <customTree :ancestralId="treeQuery.ancestralId" :btnTitle="' 创建模型规范根目录 '" @getId="getId" :type="false"
        @getTree="getTree" :expandType="'1'" />
    </el-col>
    <el-col :span="19">
      <div class="app-container">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">API名称 </span>
            <el-input v-model="listQuery.apiName" placeholder="请输入API名称" style="width: 200px" />
          </div>
          <div class="filter-item">
            <span class="span">请求方式 </span>
            <el-select v-model="listQuery.reqMethod" clearable placeholder="请选择请求方式" style="width: 200px">
              <el-option v-for="item in dictList('req_type')" :key="'type' + item.label" :label="item.label"
                :value="item.value" />
            </el-select>
          </div>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>
          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest')
            }}</el-button>
        </div>
        <el-row>
          <el-col :span="8" v-for="(o, index) in list" :key="index" style="padding: 14px">
            <el-card class="out" :body-style="{ padding: '0px' }">
              <div class="colRow" style="padding: 14px">
                <el-tooltip class="item" effect="dark" :content="o.apiName" placement="top">
                  <div><span>API名称 : </span>{{ o.apiName }}</div>
                </el-tooltip>

                <div><span>API版本 : </span>{{ o.apiVersion }}</div>
                <div><span>API路径 : </span>{{ o.apiUrl }}</div>
                <div><span>请求方式 : </span>{{ o.reqMethod | dictLabel('req_type') }}</div>
                <div><span>返回格式 : </span>{{ o.resType | dictLabel('res_type') }}</div>
                <div><span>状态 : </span>{{ o.status | dictLabel('is_release') }}</div>
                <el-tooltip class="item" effect="dark" :content="o.sourceName" placement="top">
                  <div><span>数据源 : </span>{{ o.sourceName }}</div>
                </el-tooltip>
              </div>
            </el-card>
          </el-col>
        </el-row>
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
          :page-sizes="pageArray" @pagination="getList" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { deletesource, exportsource, syncsource } from '@/api/metadata/source/source'
import { fetchMasterdataDirectoryList, deleteMasterdataDirectory } from '@/api/masterdata/masterdataDirectory'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import Pagination from '@/components/Pagination'
import Sortable from 'sortablejs'
import { fetchMarketApiList } from '@/api/marketApi/marketApi'

import customTree from '@/components/customTree/index.vue'
export default {
  name: 'sourceList',
  components: { customTree, Pagination },
  directives: { waves, permission },
  data() {
    return {
      filterText: '',
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      downloadLoading: false,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        sourceName: undefined,
        dbType: undefined,
        status: undefined,
        isSync: undefined,
        directoryId: '',
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
      treeQuery: {
        tableName: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
        ancestralId: '6',
      },
      treeList: [],
      NODE_KEY: 'id', // id对应字段
      MAX_LEVEL: 5, // 设定最大层级
      NODE_ID_START: 0, // 新增节点id，逐次递减
      startId: null,
      defaultProps: {
        // 默认设置
        children: 'children',
        label: 'name',
      },
      initParam: {
        // 新增参数
        name: '新增节点',
        parentId: 0,
        children: [],
      },
      currentNodekey: '',
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },

  watch: {
    filterText(val) {
      this.$refs.SlotTree.filter(val)
    },
  },
  methods: {
    getId(data) {
      this.listQuery.ancestralId = '6'
      this.listQuery.directoryId = data.id
      this.getList()
    },
    getTree(data) {
      this.treeList = data
    },

    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    getList() {
      this.listLoading = true
      fetchMarketApiList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      if (this.listQuery.directoryId) {
        this.$refs.form.handleCreate(this.listQuery.directoryId)
      } else {
        this.$refs.form.handleCreate(this.listQuery.directoryId)
      }
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleLook(row) {
      this.$refs.form.handleLook(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletesource(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSync(row) {
      syncsource(row.id).then((res) => {
        if (res.data.code === 0) {
          this.getList()
          this.$message.success(response.data.msg)
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletesource(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleImport() {
      this.$refs.import.show()
    },
    handleExport() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.downloadLoading = true
      exportsource(this.ids).then((response) => {
        this.downloadLoading = true
        if (response.data.code === 0) {
          import('@/vendor/Export2Excel').then((excel) => {
            excel.export_byte_to_excel(response.data.data.bytes, response.data.data.title)
            this.downloadLoading = false
          })
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    setSort() {
      const el = this.$refs.table.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        setData: function (dataTransfer) {
          // to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
          dataTransfer.setData('Text', '')
        },
        onEnd: (evt) => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)
          // TODO：这里完成自己的业务处理
        },
      })
    },
    handleFilter() {
      if (this.listQuery.directoryId) {
        this.listQuery.page = 1
        this.getList()
      } else {
        this.listQuery.page = 1
        this.getList()
      }
    },
    //重置
    handleRest() {
      this.listQuery = {
        directoryId: this.listQuery.directoryId,
        page: this.listQuery.page,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },


  },
}
</script>
<style scoped>
.el-row {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 90px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.el-card {
  width: 399px !important;
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}

.colRow img {
  width: 30%;
  height: 50%;
  right: 3%;
  /* background-size: 100% 100%; */
  position: absolute;
}

.colRow {
  position: relative;
}

.colRow div {
  width: 400px;
  font-size: 13px;
  color: #606266;
  margin: 5px;
}

.colRow div span {
  font-size: 15px;
  color: #232222;
}

.out:hover {
  z-index: 2;
  -webkit-box-shadow: 0 15px 30px rgba(14, 131, 226, 0.6);
  box-shadow: 0 15px 30px rgba(14, 131, 226, 0.6);
  -webkit-transform: translate3d(0, -2px, 0);
  transform: translate3d(0, -2px, 0);
}

.el-row {
  display: flex;
}

.el-col-5 {
  flex: 1;
}
</style>
